package com.tianqingwl.gsystem.install.controller;

import cn.hutool.core.io.FileUtil;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.ResultVo;
import com.tianqingwl.gsystem.install.pojo.InstallStatus;
import com.tianqingwl.gsystem.install.service.InstallService;
import com.tianqingwl.gsystem.install.vo.InstallInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;

@RequestMapping("/install")
@Controller
public class InstallController {
    @Autowired
    private InstallService installService;



    @RequestMapping(value = {"","/","/step.html"})
    public String step1(){
        return "manage/install/step";
    }

    @RequestMapping("/doInstall")
    @ResponseBody
    public Object doInstall(InstallInfoVo installInfoVo){
        ResultVo resultVo = new CommonResultVo();
        if (!validateInstallInfoVo(installInfoVo,resultVo)){
            return resultVo;
        }
        //获取数据库信息
        String dbHost = installInfoVo.getDbHost();
        String dbName = installInfoVo.getDbName();
        String dbPort = installInfoVo.getDbPort();
        String dbUser = installInfoVo.getDbUser();
        String dbPassword = installInfoVo.getDbPassword();

        //保存数据库配置信息到配置文件
        String dbConfigTemplate="spring:\n" +
                "  datasource:\n" +
                "    url: jdbc:mysql://host:port/dbName?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true\n" +
                "    username: dbuser\n" +
                "    password: pwd\n" +
                "logging:\n" +
                "  level:\n" +
                "   com.tianqingwl.gsystem: error";
        dbConfigTemplate=dbConfigTemplate.replaceAll("host",dbHost)
                .replaceAll("port",dbPort)
                .replaceAll("dbuser",dbUser)
                .replaceAll("pwd",dbPassword)
                .replaceAll("dbName",dbName);
        File dbConfigFile = new File(FileUtil.getAbsolutePath("config" + File.separator + "application.yml"));
        try{
            //初始化表信息
            installService.initSiteData(installInfoVo);
        }catch (Exception e){
            return CommonResultVo.fail("数据初始化失败");
        }
        try {
            File dbConfigDir = new File(FileUtil.getAbsolutePath("config"));
            if (!dbConfigDir.exists()){
                dbConfigDir.mkdir();
            }
            dbConfigFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResultVo.fail("保存配置失败");
        }
        FileUtil.writeBytes(dbConfigTemplate.getBytes(),dbConfigFile);

        //更新安装标识
        InstallStatus.setInstalled(true);
        return CommonResultVo.success();
    }

    /**
     * 校验信息
     * @param installInfoVo 要校验的信息
     * @param resultVo 结果信息
     * @return boolean 校验结果
     */
    private boolean validateInstallInfoVo(InstallInfoVo installInfoVo,ResultVo resultVo){
        if (installInfoVo==null){
            resultVo = CommonResultVo.fail("installInfo is null");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDbHost())){
            resultVo = CommonResultVo.fail("请输入数据库主机地址");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDbPort())){
            resultVo = CommonResultVo.fail("请输入数据库端口");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDbUser())){
            resultVo = CommonResultVo.fail("请输入数据库用户名称");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDbPassword())){
            resultVo = CommonResultVo.fail("请输入数据库用户密码");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDbName())){
            resultVo = CommonResultVo.fail("请输入数据库名称");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getSiteName())){
            resultVo = CommonResultVo.fail("请输入网站名称");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getDomain())){
            resultVo = CommonResultVo.fail("请输入网站域名");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getManageShowName())){
            resultVo = CommonResultVo.fail("请输入管理员名称");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getManageUsername())){
            resultVo = CommonResultVo.fail("请输入管理员账号");
            return false;
        }
        if (StringUtils.isEmpty(installInfoVo.getManagePassword())){
            resultVo = CommonResultVo.fail("请输入管理员密码");
            return false;
        }
        return true;
    }
}
